package com.yingli.fragment.manager;

import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.yingli.fragment.R;
import com.yingli.fragment.manager.fragment.ChatFragment;
import com.yingli.fragment.manager.fragment.ContactFragment;
import com.yingli.fragment.manager.fragment.FindFragment;
import com.yingli.fragment.manager.fragment.MineFragment;

public class ManagerActivity extends AppCompatActivity {
    private TextView tvChat;
    private TextView tvContact;
    private TextView tvFind;
    private TextView tvMy;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private ChatFragment chatFragment;
    private ContactFragment contactFragment;
    private FindFragment findFragment;
    private MineFragment mineFragment;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager);

        //操作Fragment，需要manager对象去操作每个fragment，获取manager对象
        manager = getSupportFragmentManager();
        //开启管理事务
        transaction = manager.beginTransaction();
        //实例化每个Fragment片段
        chatFragment = new ChatFragment();
        findFragment = new FindFragment();
        contactFragment = new ContactFragment();
        mineFragment = new MineFragment();
        //事务添加Fragment，把Fragment添加到容器（FrameLayout）中
        transaction.add(R.id.fl_container,chatFragment);
        //提交事务
        transaction.commit();
        tvChat = findViewById(R.id.tv_message);
        tvContact = findViewById(R.id.tv_contact);
        tvFind = findViewById(R.id.tv_find);
        tvMy = findViewById(R.id.tv_my);
        tvChat.setOnClickListener(v->{
            changeToChat();
        });
        tvContact.setOnClickListener(v->{
            changeToContact();
        });
        tvFind.setOnClickListener(v->{
            changeToFind();
        });
        tvMy.setOnClickListener(v->{
            changeToMy();
        });
    }

    //跳转到发现界面
    private void changeToFind() {
        transaction = manager.beginTransaction();
        transaction.replace(R.id.fl_container,findFragment);
        transaction.commit();
    }

    //跳转到我的
    private void changeToMy() {
        transaction = manager.beginTransaction();
        transaction.replace(R.id.fl_container, mineFragment);
        transaction.commit();
    }

    //跳转到联系人界面
    private void changeToContact() {
        transaction = manager.beginTransaction();
        transaction.replace(R.id.fl_container,contactFragment);
        transaction.commit();
    }

    //跳转到聊天界面
    private void changeToChat() {
        transaction = manager.beginTransaction();
        transaction.replace(R.id.fl_container,chatFragment);
        transaction.commit();
    }
}
